package Service.impl;

import Mapper.SnackMapper;
import Mapper.SnackOrderMapper;
import Pojo.Order;
import Pojo.Pay;
import Pojo.Snack;
import Pojo.SnackOrder;
import Service.SnackOrderService;
import Util.AliPayUtil2;
import Util.BaseMapper;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.demo.trade.model.result.AlipayF2FPrecreateResult;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

public class SnackOrderServiceImpl implements SnackOrderService {
    private SnackOrderMapper snackOrderMapper= BaseMapper.getSqlSession().getMapper(SnackOrderMapper.class);
    private SnackMapper snackMapper= BaseMapper.getSqlSession().getMapper(SnackMapper.class);
    @Override
    public Integer SnackSumPrice(Integer num, Integer price) {
        Integer sum=price*num;
        return sum;
    }

    @Override
    public HashMap<Object,Object> AddOne(SnackOrder snackOrder) {
        HashMap<Object,Object> map=new HashMap<>();
        //时间生成
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        String sotime = simpleDateFormat.format(date);
        snackOrder.setSotime(sotime);
        //去新增订单
        Integer integer=snackOrderMapper.AddOne(snackOrder);
        map.put("addResult",integer);
        map.put("snackorder",snackOrder);//传入食品订单对象供后续查询支付使用
        //添加至支付宝pay类中
        Pay pay=new Pay();
        pay.setOut_trade_no(String.valueOf(snackOrder.getSoid()));
        pay.setStore_id("dh233");
        pay.setBody("");
        pay.setSubject("食品购买");
        pay.setScene("线上购买");
        pay.setTotal_amount(String.valueOf(snackOrder.getAllprice()));
        pay.setTimeout_express("1m");
        AlipayF2FPrecreateResult result= AliPayUtil2.tradePrecreate(pay);
        switch (result.getTradeStatus()){
            case SUCCESS:
                System.out.println("生成成功");
                AlipayTradePrecreateResponse response=result.getResponse();
                map.put("qrResult",response.getQrCode());
                break;
            case UNKNOWN:
                System.out.println("生成异常");
                map.put("qrResult","生成异常");
                break;
            case FAILED:
                System.out.println("生成失败");
                map.put("qrResult","生成失败");
                break;
            default:
                System.out.println("异常");
                break;
        }
        return map;
    }
//
    @Override
    public String PaySnackStatusQuery(String snackOrderId,String snackId) {
        String s = AliPayUtil2.TestTradeQuery(snackOrderId);
        if(s.equals("success")){
            //库存变化，零食表库存修改
            //先去查询当前零食的库存,并用当前剩余库存减去用户购买的数量
            Snack snack=snackMapper.QueryById(Integer.valueOf(snackId));
            SnackOrder snackOrder=snackOrderMapper.QueryById(Integer.parseInt(snackOrderId));
            Integer oldStock=snack.getStock();
            Integer num=snackOrder.getNum();
            Integer stock=oldStock-num;
            //去修改
            snack.setStock(stock);
            Integer integer1=snackMapper.UpdateStockById(snack);
            return "success";
        }
        return "fail";
    }
}
